package com.yy.show.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.yy.base.common.service.BaseService;
import com.yy.base.util.ObjectUtil;
import com.yy.module.entity.ModuleField;
import com.yy.module.entity.ModuleInfo;
import com.yy.module.entity.ModuleQueryField;
import com.yy.show.util.ModuleDeleteHandle;
import com.yy.show.util.ModuleQueryHandle;
import com.yy.show.util.ModuleSaveHandle;
import com.yy.show.util.ModuleTreeHandle;

@Service("showService")
@SuppressWarnings("unchecked")
public class ShowService extends BaseService {
	/**
	 * 查询表格数据
	 */
	public List getQueryList(ModuleQueryHandle handle) {
		String sql = "SELECT * FROM (" + handle.getQuerySql() + ") TB";
		return dao.query(sql, handle.getParamMap());
	}
	/**
	 * 分页查询表格数据
	 */
	public List splitQueryList(ModuleQueryHandle handle) {
		String sql = "SELECT * FROM (" + handle.getQuerySql() + ") TB";
		return dao.splitQuery(sql, handle.getParamMap());
	}
	/**
	 * 根据ID取单条数据内容
	 */
	public Map<String, Object> getModuleData(ModuleQueryHandle handle) {
		String sql = "SELECT * FROM (" + handle.getQuerySql() + ") TB";
		List<Map<String, Object>> list = dao.query(sql, handle.getParamMap());
		return list.size() > 0 ? list.get(0) : null;
	}
	/**
	 * 获取页面树形结构数据
	 */
	public Map getTreeData(ModuleTreeHandle handle) {
		String sql = "SELECT * FROM (" + handle.getSql() + ") TB";
		return handle.queryDataToMapTree(dao.query(sql, handle.getParamMap()));
	}
	
	/**
	 * 初始化页面信息，获取所有模块
	 */
	public Map<String, ModuleInfo> initPage(String code) {
		Map<String, ModuleInfo> map = new HashMap<String, ModuleInfo>();
		ModuleInfo module = (ModuleInfo)dao.findUnique("from ModuleInfo where isActive = 'Y' and code = ?", code);
		if (module == null) {
			return map;
		}
		map.put(module.getCode(), module);
		List<ModuleInfo> result = dao.find("from ModuleInfo where isActive = 'Y' and parentId = ?", module.getId());
		for (int i = 0; i < result.size(); i++) {
			map.put(result.get(i).getCode(), result.get(i));
		}
		return map;
	}
	/**
	 * 根据编号查询模型信息
	 */
	public ModuleInfo getModuleByCode(String code) {
		ModuleInfo module = (ModuleInfo)dao.findUnique("from ModuleInfo where isActive = 'Y' and code = ?", code);
		if (module != null) {
			List list = dao.query(
					"select (select t.name from t_table t where t.isActive = 'Y' and t.id = m.tableId) as tableName," +
					"(select f.isOnly from t_field f where f.id = m.columnId) as isOnly," +
					"m.* from t_module_field m where isActive = 'Y' and m.moduleId = ? order by m.orderBy", module.getId());
			module.setFieldList(ObjectUtil.listMapToEntity(ModuleField.class, list));
			
			list = dao.query("select (select t.name from t_table t where t.isActive = 'Y' and t.id = m.tableId) as tableName," +
					"m.* from t_module_query m where isActive = 'Y' and m.moduleId = ? order by m.orderBy", module.getId());
			module.setQueryFieldList(ObjectUtil.listMapToEntity(ModuleQueryField.class, list));
		}
		return module;
	}
	/**
	 * 查询模块展示信息
	 */
	public List getModuleFieldList(String moduleId) {
		return ObjectUtil.listMapToEntity(ModuleField.class, dao.query(
				"select (select t.name from t_table t where t.isActive = 'Y' and t.id = m.tableId) as tableName," +
				"m.* from t_module_field m where isActive = 'Y' and m.moduleId = ? order by m.orderBy", moduleId));
	}
	/**
	 * 查询模块展示信息
	 */
	public List getModuleQueryList(String moduleId) {
		return ObjectUtil.listMapToEntity(ModuleQueryField.class, dao.query(
				"select (select t.name from t_table t where t.isActive = 'Y' and t.id = m.tableId) as tableName," +
				"m.* from t_module_query m where isActive = 'Y' and m.moduleId = ? order by m.orderBy", moduleId));
	}
	/**
	 * 保存数据
	 */
	public void saveData(ModuleSaveHandle handle) {
		dao.executeSql(handle.getSql(), handle.getParamMap());
	}
	/**
	 * 删除数据
	 */
	public void deleteData(ModuleDeleteHandle handle) {
		dao.executeSql(handle.getSql(), handle.getParamMap());
	}
}